package com.jzy.message.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jzy.message.R;
import com.jzy.message.activities.base.FragmentAdapter;
import com.jzy.message.activities.base.BaseFragment;
import com.jzy.message.control.popmenu.PopMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/20.
 */
public class MsgFragment extends BaseFragment implements View.OnClickListener {

    private TabLayout tlayout_message_tab;
    private ViewPager vp_message_content_message;
    private ViewPager vp_message_content_im;
    private Button btn_message_title;
    private ImageView iv_message_title;
    private PopMenu popMenu;

    private LinearLayout llayout_message_fragment_following;
    private LinearLayout llayout_message_fragment_contact;

    private Button btn_message_following;
    private Button btn_message_me;
    private Button btn_message_contact;
    private Button btn_message_message;

    private String[] tabs_Message={ "关注", "粉丝"};
    private Button[] mBtnTabs_Message = new Button[tabs_Message.length];
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.message_fragment_inhome, container, false);
        initView(view);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //setupTabsInDynamicTab();
    }
    private void initView(View view)
    {
        tlayout_message_tab=(TabLayout) view.findViewById(R.id.tlayout_message_tab);
        vp_message_content_message=(ViewPager)view.findViewById(R.id.vp_message_content_message);
        vp_message_content_im=(ViewPager)view.findViewById(R.id.vp_message_content_im);
        btn_message_title=(Button)view.findViewById(R.id.btn_message_title);
        iv_message_title=(ImageView)view.findViewById(R.id.iv_message_title);

        setupTabsInMessageTab("圈子");
        //设置响应事件
        btn_message_title.setOnClickListener(this);
        iv_message_title.setOnClickListener(this);

        llayout_message_fragment_contact=(LinearLayout)view.findViewById(R.id.llayout_message_fragment_contact);
        llayout_message_fragment_following=(LinearLayout)view.findViewById(R.id.llayout_message_fragment_following);
        btn_message_following=(Button)view.findViewById(R.id.btn_message_following);
        btn_message_me=(Button)view.findViewById(R.id.btn_message_me);
        btn_message_contact=(Button)view.findViewById(R.id.btn_message_contact);
        btn_message_message=(Button)view.findViewById(R.id.btn_message_message);
        btn_message_following.setOnClickListener(this);
        btn_message_me.setOnClickListener(this);
        btn_message_contact.setOnClickListener(this);
        btn_message_message.setOnClickListener(this);

    }
/*    public void setCurrentViewPager(int index)
    {
        vp_message_content.setCurrentItem(index);
    }*/
    private void setupTabsInMessageTab(String flag) {
        //初始化TabLayout的title数据集
        List<String> titles = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        switch (flag)
        {
            case "圈子":
                titles.add("关注");
                titles.add("粉丝");
                tlayout_message_tab.removeAllTabs();
                //初始化TabLayout的title
                tlayout_message_tab.addTab(tlayout_message_tab.newTab().setText(titles.get(0)));
                tlayout_message_tab.addTab(tlayout_message_tab.newTab().setText(titles.get(1)));
                //初始化ViewPager的数据集
                fragments.add(new Message_FollowingFragment());
                fragments.add(new Message_MeFragment());
                break;
            case "通信":
                titles.add("好友列表");
                titles.add("消息记录");
                //初始化TabLayout的title
                tlayout_message_tab.removeAllTabs();
                tlayout_message_tab.addTab(tlayout_message_tab.newTab().setText(titles.get(0)));
                tlayout_message_tab.addTab(tlayout_message_tab.newTab().setText(titles.get(1)));
                //初始化ViewPager的数据集
                fragments.add(new Message_ContactFragment());
                fragments.add(new Message_MessageFragment());
                break;
            default:
                break;
        }
        //创建ViewPager的adapter
        FragmentAdapter adapter = new FragmentAdapter(getFragmentManager(), fragments, titles);
        vp_message_content_message.setAdapter(adapter);
        //关联TabLayout与ViewPager
        //同时也要覆写PagerAdapter的getPageTitle方法，否则Tab没有title
        tlayout_message_tab.setupWithViewPager(vp_message_content_message);
//        tlayout_message_tab.setTabsFromPagerAdapter(adapter);
    }
    private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0)
        {
            //mTabWidget.setCurrentTab(arg0);
            //vp_home_blog.setCurrentItem(arg0);
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {

        }
        @Override
        public void onPageScrollStateChanged(int arg0)
        {

        }
    };
    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.btn_message_title||id==R.id.iv_message_title)
        {
            popMenu = new PopMenu(getActivity());
            popMenu.addItems(new String[]{"圈子","通信"});
            popMenu.setOnItemClickListener(popmenuItemClickListener);
            popMenu.showAsDropDown(btn_message_title);
        }
        else if(id==R.id.btn_message_following)
        {
            btn_message_following.setBackgroundResource(R.drawable.btn_shape_radius_pressed);
            btn_message_following.setTextColor(Color.WHITE);
            btn_message_me.setBackgroundResource(R.color.transparent);
            btn_message_me.setTextColor(Color.BLACK);
            vp_message_content_message.setCurrentItem(0);
        }
        else if(id==R.id.btn_message_me)
        {
            btn_message_me.setBackgroundResource(R.drawable.btn_shape_radius_pressed);
            btn_message_me.setTextColor(Color.WHITE);
            btn_message_following.setBackgroundResource(R.color.transparent);
            btn_message_following.setTextColor(Color.BLACK);
            vp_message_content_message.setCurrentItem(1);
        }
        else if(id==R.id.btn_message_contact)
        {
            btn_message_contact.setBackgroundResource(R.drawable.btn_shape_radius_pressed);
            btn_message_contact.setTextColor(Color.WHITE);
            btn_message_message.setBackgroundResource(R.color.transparent);
            btn_message_message.setTextColor(Color.BLACK);
            vp_message_content_message.setCurrentItem(0);
        }
        else if(id==R.id.btn_message_message)
        {
            btn_message_message.setBackgroundResource(R.drawable.btn_shape_radius_pressed);
            btn_message_message.setTextColor(Color.WHITE);
            btn_message_contact.setBackgroundResource(R.color.transparent);
            btn_message_contact.setTextColor(Color.BLACK);
            vp_message_content_message.setCurrentItem(1);
        }
    }
    // 弹出菜单监听器
    AdapterView.OnItemClickListener popmenuItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //System.out.println("下拉菜单点击" + position);
            switch (position)
            {
                case 0:
                    setupTabsInMessageTab("圈子");
                    btn_message_title.setText("圈子");
                    llayout_message_fragment_following.setVisibility(View.VISIBLE);
                    llayout_message_fragment_contact.setVisibility(View.GONE);
                    break;
                case 1:
                    setupTabsInMessageTab("通信");
                    btn_message_title.setText("通信");
                    llayout_message_fragment_following.setVisibility(View.GONE);
                    llayout_message_fragment_contact.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
            popMenu.dismiss();
        }
    };
}
